package com.ruoyi.web.controller.userCard;

import com.ruoyi.card.domain.Card;
import com.ruoyi.card.service.ICardService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.UserCard;
import com.ruoyi.system.service.IUserCardService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户和会员卡Controller
 * 
 * @author ruoyi
 * @date 2025-06-02
 */
@RestController
@RequestMapping("/userCard/userCard")
public class UserCardController extends BaseController
{
    @Autowired
    private IUserCardService userCardService;
    @Autowired
    private ICardService cardService;
    /**
     * 查询用户和会员卡列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCard userCard)
    {
        startPage();
        List<UserCard> list = userCardService.selectUserCardList(userCard);
        return getDataTable(list);
    }

    /**
     * 导出用户和会员卡列表
     */
    @PreAuthorize("@ss.hasPermi('system:card:export')")
    @Log(title = "用户和会员卡", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCard userCard)
    {
        List<UserCard> list = userCardService.selectUserCardList(userCard);
        ExcelUtil<UserCard> util = new ExcelUtil<UserCard>(UserCard.class);
        util.exportExcel(response, list, "用户和会员卡数据");
    }

    /**
     * 获取用户和会员卡详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:card:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userCardService.selectUserCardById(id));
    }

    /**
     * 新增用户和会员卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:add')")
    @Log(title = "用户和会员卡", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCard userCard)
    {
        return toAjax(userCardService.insertUserCard(userCard));
    }

    /**
     * 修改用户和会员卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:edit')")
    @Log(title = "用户和会员卡", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCard userCard)
    {
        return toAjax(userCardService.updateUserCard(userCard));
    }

    /**
     * 删除用户和会员卡
     */
    @PreAuthorize("@ss.hasPermi('system:card:remove')")
    @Log(title = "用户和会员卡", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userCardService.deleteUserCardByIds(ids));
    }
//根据用户Id获取会员卡,并筛选符合当前房间的卡
    @GetMapping("/getCardsByUser")
    public TableDataInfo getCardsByUser(@RequestParam Long gameTypeId){
        Long userId = SecurityUtils.getUserId();
        UserCard userCard = new UserCard();
        userCard.setUserId(userId);
        List<UserCard> userCards = userCardService.selectUserCardList(userCard);
        ArrayList<Card> list = new ArrayList<>();
        for (UserCard card : userCards) {
            Long cardId = card.getCardId();
            Card card1 = cardService.selectCardById(cardId);
            if (card1.getgameTypeId()==gameTypeId){
                if (card1.getType()==2&&card1.getCount()>0||card1.getType()==1){
                    list.add(card1);
                }

            }
        }
        return getDataTable(list);

    }
//    根据用户Id获取用户拥有的会员卡
    @GetMapping("/userCards")
    public TableDataInfo getUserCards(){
        Long userId = SecurityUtils.getUserId();
        UserCard userCard = new UserCard();
        userCard.setUserId(userId);
        ArrayList<Card> cards = new ArrayList<>();
        List<UserCard> userCards = userCardService.selectUserCardList(userCard);
        for (UserCard card : userCards) {
            Long cardId = card.getCardId();
            Card card1 = cardService.selectCardById(cardId);
            cards.add(card1);
        }
        return getDataTable(cards);
    }
//    用户新增会员卡
    @PostMapping("/addUserCard")
    public AjaxResult addUserCard(@RequestBody UserCard userCard){
        Long userId = SecurityUtils.getUserId();
        userCard.setUserId(userId);
        return toAjax(userCardService.insertUserCard(userCard));
    }
}
